package state.component;


import state.Clip;
import state.Graphics;
import state.Manager;
import state.util.Color;

public class ScrollBar extends Clip {
  private int bgCorlor = Color.WHITE_CODE;
  
  private int pointerColor = Color.RED_CODE;
  
  private int pointerHeight = 10;
  
  private Clip reuseClip = new Clip(0, 0, 0, 0);
  
  private double percent;
  
  private int minTextY = Integer.MIN_VALUE;
  
  private int beginY = Integer.MAX_VALUE;
  
  private Clip view;
  
  private int lastY = Integer.MIN_VALUE;
  
  public ScrollBar(int x, int y, int width, int height, Clip view, Manager manager) {
    super(x, y, width, height);
    this.view = view;
    manager.addScrollBar(this);
  }
  
  public void draw(Graphics g) {
    Clip.getClip(g, reuseClip);
    clipRect(g);
    
    g.setColor(bgCorlor);
    g.fillRect(x, y, width, height);
    
    g.setColor(pointerColor);
    g.fillRect(x, y + (int) ((height - pointerHeight) * percent / 100), width, pointerHeight);
    
    reuseClip.applyClip(g);
  }

  public int getBgCorlor() {
    return bgCorlor;
  }

  public ScrollBar setBgCorlor(int bgCorlor) {
    this.bgCorlor = bgCorlor;
    return this;
  }

  public int getPointerColor() {
    return pointerColor;
  }

  public ScrollBar setPointerColor(int pointerColor) {
    this.pointerColor = pointerColor;
    return this;
  }

  public int getPointerHeight() {
    return pointerHeight;
  }

  public ScrollBar setPointerHeight(int pointerHeight) {
    this.pointerHeight = pointerHeight;
    return this;
  }

  public double getPercent() {
    return percent;
  }
  
  public boolean canScrollDown() {
    return percent > 0;
  }
  
  public boolean canScrollUp() {
    return percent < 100;
  }

  public void setPercent(double percent) {
    if (percent > 100) {
      percent = 100;
    } else if (percent < 0) {
      percent = 0;
    }
    this.percent = percent;
  }

  public int getMinTextY() {
    return minTextY;
  }

  public int getBeginY() {
    return beginY;
  }

  public void setBeginY(int beginY) {
    if (beginY < this.beginY) {
      this.beginY = beginY;
    }
  }
  
  public int getLastY() {
    return lastY;
  }

  public void setLastY(int lastY) {
    if (lastY > this.lastY) {
      this.lastY = lastY;
    }
  }
  
  public Clip getView() {
    return view;
  }

  public void setView(Clip view) {
    this.view = view;
  }

  public void updateScroll(int inputBeginY, int inputLastY) {
    setBeginY(inputBeginY);
    setLastY(inputLastY);
    
    int allHeight = lastY - beginY;
    int heightToCalculateScroll = allHeight - view.height;
    if (heightToCalculateScroll < 0) {
      heightToCalculateScroll = 1;
    }
    int scrolledDistance = view.y - beginY;
    setPercent(scrolledDistance * 100.0 / heightToCalculateScroll);
    minTextY = view.y - heightToCalculateScroll;
  }

  public void reset() {
    beginY = Integer.MAX_VALUE;
    lastY = Integer.MIN_VALUE;
  }
}
